package com.lagou.homework02;

import java.util.Random;

public class Demo01 {

    public static void main(String[] args) {
        //1.定义一个长度为[16][16]的整型二维数组，并随机生成所有位置的元素值（1-100）
        int[][] nums = new int[16][16];
        Random r1 = new Random(10);
        //int d1 = r1.nextInt(100)+1;
        for (int i = 0; i < nums.length; i++) {
            for(int j =0;j<nums[i].length;j++){
                int d1 = r1.nextInt(100)+1;
                nums[i][j] = d1;
                System.out.println(nums[i][j]);
            }
        }
        System.out.println("-------------------------------");
        //2.分别实现二维数组每行元素的累加和及每列元素的累加和并打印
        int[] width = new int[16];//行
        int[] l1 = new int[16];//列
        for (int i = 0; i < nums.length; i++) {
            for(int j =0;j<nums[i].length;j++){
                width[j]+=nums[i][j];
            }
        }
        for (int i = 0; i < nums.length; i++) {
            for(int j =0;j<nums[i].length;j++){
                l1[i]+=nums[i][j];
            }
        }
        for (int i = 0; i < width.length; i++) {
            System.out.println("第"+i+"行的累加和是"+width[i]);
        }
        for (int i = 0; i < l1.length; i++) {
            System.out.println("第"+i+"列的累加和是"+l1[i]);
        }
        System.out.println("-----------------------------");
        //3.再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印
        int a1 = 0;
        int a2 = 0;
        for (int i = 0; i < nums.length; i++) {
            for(int j =0;j<nums[i].length;j++){
                if(i==j){
                    a1+=nums[i][j];
                }
                if(i+j==15){
                    a2+=nums[i][j];
                }
            }
        }
        System.out.println("左上角到右下角的累加和:"+a1);
        System.out.println("右上角到左下角的累加和:"+a2);
    }
}
